package net.lab1024.sa.admin.module.business.supercomputer.sqlgenerator.service;

import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.supercomputer.sqlgenerator.constant.SQLTypeToFlinkTypeConstants;
import net.lab1024.sa.admin.module.business.supercomputer.sqlgenerator.domain.entity.DataConfigEntity;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * SQL模版处理类
 */
@Slf4j
@Service
public class SqlGeneratorTemplateService {
    public Map<String, String> sqlToFlink(Map<String, String> columns){
        Map<String, String> flinkTypes = new HashMap<>();

        for (Map.Entry<String, String> entry : columns.entrySet()) {
            String columnName = entry.getKey();
            String sqlType = entry.getValue();

            try {
                // 使用 SQL 类型转换为 Java 类型
                String javaType = SQLTypeToFlinkTypeConstants.sqlToFlinkType(sqlType);
                flinkTypes.put(columnName, javaType);
            } catch (IllegalArgumentException e) {
                // 处理不支持的 SQL 类型（可以选择记录日志或抛出异常）
                System.err.println("Unsupported SQL type for column " + columnName + ": " + sqlType);
            }
        }

        return flinkTypes;
    }

    public String getSource(DataConfigEntity dataConfigEntity, Map<String, String> columns){
        // 1. 设置velocity资源加载器
        VelocityEngine engine = new VelocityEngine();
        engine.setProperty(Velocity.RESOURCE_LOADER, "classpath");
        engine.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        engine.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

        // 2. 初始化velocity引擎
        engine.init();

        // 3. 创建velocity容器
        VelocityContext context = new VelocityContext();
        context.put("DataConfig", dataConfigEntity); // 确保命名和模板相符
        context.put("map", columns); // 将columns放入上下文以便在模板中使用

        // 4. 加载模板
        Template template = engine.getTemplate("vms/SQL/source.vm");

        // 5. 合并数据到模版并输出到StringWriter
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        // 6. 返回合并后的字符串
        return writer.toString();
    }
    public String getSink(DataConfigEntity dataConfigEntity, Map<String, String> columns,String path){
        // 1. 设置velocity资源加载器
        VelocityEngine engine = new VelocityEngine();
        engine.setProperty(Velocity.RESOURCE_LOADER, "classpath");
        engine.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        engine.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

        // 2. 初始化velocity引擎
        engine.init();

        // 3. 创建velocity容器
        VelocityContext context = new VelocityContext();
        context.put("DataConfig", dataConfigEntity); // 确保命名和模板相符
        context.put("map", columns); // 将columns放入上下文以便在模板中使用
        context.put("path",path);//传入目的路径

        // 4. 加载模板
        Template template = engine.getTemplate("vms/SQL/sink.vm");

        // 5. 合并数据到模版并输出到StringWriter
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        // 6. 返回合并后的字符串
        return writer.toString();
    }
}
